package com.wushi.lenovo.asia5b.app_ui.good.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.mvp.base.BaseMvpActivity;
import com.mvp.utils.DisplayUtils;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.AppExtendsKt;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.goods.mvpview.ThirdMallActivityView;
import com.wushi.lenovo.asia5b.app_di.goods.presenter.ThirdMallActivityPresenter;
import com.wushi.lenovo.asia5b.app_model.good.ThridMallModel;
import com.wushi.lenovo.asia5b.app_ui.good.adapter.ThirdMallAdapter;
import com.wushi.lenovo.asia5b.app_ui.h5.ThirdContentActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangyuncai on 2019/3/16.
 * 第三方平台选择(淘宝/天猫等)
 */
public class ThirdMallActivity extends BaseMvpActivity<ThirdMallActivityView, ThirdMallActivityPresenter> implements ThirdMallActivityView {
    @BindView(R.id.rv_thirdmall)
    RecyclerView mRecyclerView;
    final List<ThridMallModel> imgs = new ArrayList<>();
    private ThirdMallAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        setStatusTrans();
        return R.layout.activity_thirdmall;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {

        mAdapter = new ThirdMallAdapter(imgs);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mRecyclerView.setAdapter(mAdapter);
        GridSpacingItemDecoration gridSpacingItemDecoration = new GridSpacingItemDecoration(3, DisplayUtils.dip2px(this, 10), true);
        mRecyclerView.addItemDecoration(gridSpacingItemDecoration);
    }

    @Override
    protected void initData() {
        List<String> bThridSources = AppExtendsKt.getBThridSources();
        if (bThridSources != null) {
            for (String source : bThridSources) {
                ThridMallModel thridMallModel = new ThridMallModel();
                thridMallModel.setSource(source);
                thridMallModel.initOtherValue();
                if (!TextUtils.isEmpty(thridMallModel.getTitle())) {
                    imgs.add(thridMallModel);
                }
            }
        }
        if (imgs.size() > 0) {
            mAdapter.notifyDataSetChanged();
        } else {
            getPresenter().getShopFromglobeListing();
        }
    }

    @Override
    protected void initEvent() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ThridMallModel item = mAdapter.getItem(position);
                if (item != null) {
                    ThirdContentActivity.startThirdContentActivity(ThirdMallActivity.this,
                            getString(item.getUrl()), item.getTitle());
                }
            }
        });
    }


    @Override
    public void getShopFromglobeListingResult(List<ThridMallModel> data) {
        if (data != null) {
            imgs.clear();
            List<String> bThridSources = new ArrayList<>();
            for (ThridMallModel thridMallModel : data) {
                thridMallModel.initOtherValue();
                if (!TextUtils.isEmpty(thridMallModel.getTitle())) {
                    bThridSources.add(thridMallModel.getSource());
                    imgs.add(thridMallModel);
                }
            }
            //缓存起来
            AppExtendsKt.setBThridSources(bThridSources);
            mAdapter.notifyDataSetChanged();
        }
    }
}
